﻿// ******************************************************************
// * Copyright(c)  Steven Lu
// * Description   : Entity_PROD_COST_COEFFICIENT
// * CreateDate    : 2011-11-11 16:00:21
// * Creater       : CodeSmith
// * LastChangeDate: 2011-11-11 16:00:21
// * LastChanger   : 2011-11-11 16:00:21
// * Version Info  : 1.0 For ESOL
// ******************************************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Suzsoft.Smart.Data;
using System.Data;

using ESOL.Common;
using ESOL.Common.Entity;

namespace ESOL.Business.Prod
{
    /// <summary>
    /// ProdCostCoefficient
    /// </summary>
    public partial class ProdCostCoefficientBs
    {
        #region GetByID
        /// <summary>
        /// Get Entity
        /// </summary>
        /// <param name="objProdCostCoefficientEntity">Entity</param>
        /// <returns>Entity</returns>
        public static ProdCostCoefficientEntity GetProdCostCoefficient(ProdCostCoefficientEntity objProdCostCoefficientEntity)
        {
            return DataAccess.SelectSingle<ProdCostCoefficientEntity>(objProdCostCoefficientEntity);
        }
        #endregion


        #region Insert
        /// <summary>
        /// Insert Data
        /// </summary>
        /// <param name="objProdCostCoefficientEntity">Entity</param>
        /// <returns>true Insert Succeed; false Insert Fail</returns>
        public static bool InsertProdCostCoefficient(ProdCostCoefficientEntity objProdCostCoefficientEntity)
        {
            return DataAccess.Insert(objProdCostCoefficientEntity);
        }

        public static bool InsertProdCostCoefficient(List<ProdCostCoefficientEntity> objProdCostCoefficientEntityList)
        {
            return DataAccess.Insert(objProdCostCoefficientEntityList);
        }
        #endregion


        #region Update
        /// <summary>
        /// Update Data
        /// </summary>
        /// <param name="objProdCostCoefficientEntity">Entity</param>
        /// <returns>true Update Succeed; false Update Fail</returns>
        public static bool UpdateProdCostCoefficient(ProdCostCoefficientEntity objProdCostCoefficientEntity)
        {
            return DataAccess.Update(objProdCostCoefficientEntity);
        }

        public static bool UpdateProdCostCoefficient(List<ProdCostCoefficientEntity> objProdCostCoefficientEntityList)
        {
            return DataAccess.Update<ProdCostCoefficientEntity>(objProdCostCoefficientEntityList);
        }
        #endregion


        #region Delete
        /// <summary>
        /// Delete Data
        /// </summary>
        /// <param name="objProdCostCoefficientEntity">Entity</param>
        /// <returns>true Delete Succeed; false Delete Fail</returns>
        public static bool DeleteProdCostCoefficient(ProdCostCoefficientEntity objProdCostCoefficientEntity)
        {
            return DataAccess.Delete(objProdCostCoefficientEntity);
        }

        public static bool DeleteProdCostCoefficient(List<ProdCostCoefficientEntity> objProdCostCoefficientEntityList)
        {
            return DataAccess.Delete<ProdCostCoefficientEntity>(objProdCostCoefficientEntityList);
        }
        #endregion


        #region GetAll
        /// <summary>
        /// Get All Data
        /// </summary>
        /// <returns>Back All Data<returns>
        public static DataSet GetProdCostCoefficientAll()
        {
            return DataAccess.SelectAllDataSet<ProdCostCoefficientEntity>();
        }
        /// <summary>
        /// Get All Data List
        /// </summary>
        /// <returns>Back All Data List<returns>
        public static List<ProdCostCoefficientEntity> GetProdCostCoefficientAllList()
        {
            return DataAccess.SelectAll<ProdCostCoefficientEntity>();
        }
        #endregion

        #region Functions
        public static DataTable GetDtCostCoefficientByProductID(string ProductID)
        {
            if(string.IsNullOrEmpty(ProductID))
                return null;
            string Sql = @"SELECT PCO.*,MP1.NAME_CN AS CREATE_BY_NAME,MP2.NAME_CN AS APPROVED_BY_NAME,MP.PRODUCT_CODE 
                FROM PROD_COST_COEFFICIENT PCO
                LEFT JOIN MST_PERSON MP1 ON PCO.CREATE_BY=MP1.PERSON_ID
                LEFT JOIN MST_PERSON MP2 ON PCO.APPROVED_BY=MP2.PERSON_ID 
                LEFT JOIN MST_PRODUCT MP ON PCO.PRODUCT_ID=MP.PRODUCT_ID
                WHERE PCO.PRODUCT_ID='" + ProductID+"' ";
            DataSet ds=DataAccess.SelectDataSet(Sql);
            if (ds != null && ds.Tables[0] != null)
                return ds.Tables[0];
            else return null;
        }

        public static bool SaveCostCoefficient(DataTable dt)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    List<ProdCostCoefficientEntity> insertList = new List<ProdCostCoefficientEntity>();
                    List<ProdCostCoefficientEntity> modifyList = new List<ProdCostCoefficientEntity>();
                    foreach (DataRow dr in dt.Rows)
                    {
                        if (dr.RowState == DataRowState.Added)
                        {
                            ProdCostCoefficientEntity m = new ProdCostCoefficientEntity();
                            m.CostCofficientId = Guid.NewGuid().ToString();
                            m.ProductId = dr["PRODUCT_ID"].ToString();
                            m.CostCofficient = Convert.ToDecimal(dr["COST_COFFICIENT"].ToString());
                            m.CreateBy = Common.CommonParameter.CurrentUser.PersonId;
                            m.CreateDate = DateTime.Today;
                            m.Status = dr["STATUS"].ToString();
                            insertList.Add(m);
                        }
                        else if (dr.RowState == DataRowState.Modified)
                        {
                            ProdCostCoefficientEntity m = new ProdCostCoefficientEntity(dr);
                            m.CostCofficient = Convert.ToDecimal(dr["COST_COFFICIENT"].ToString());
                            m.Status = dr["STATUS"].ToString();
                            modifyList.Add(m);
                        }
                    }
                    DataAccess.Update<ProdCostCoefficientEntity>(modifyList, broker);
                    DataAccess.Insert<ProdCostCoefficientEntity>(insertList, broker);
                    broker.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    throw ex;
                }
            }
        }
        #endregion
    }


}

